Ext.namespace('SLM');

SLM.EventsPanel = function(config) {

	this.grid = new SLM.EventsGrid();

	this.matrix = new SLM.EventsMatrix();

	this.frm = new SLM.EventsForm({
			region: 'east'
	});

	this.frm.on('filtersChanged', function() {
		this.grid.store.reload();
		this.matrix.store.reload();
	}, this);
	
	this.currentItem = 0;
	
	var card = new Ext.Panel({
			layout:'card'
		,	id: 'events-card'
		, 	iconCls: 'icon_events'
		,	region: 'center'
		,	activeItem: this.currentItem
		,	layoutConfig: {
				deferredRender: false
			}
		,	border: false
		,	defaults: {
				border:false
			}
		,	tbar: [{
					text: 'Add Event'
				,	id: 'btn-add-event'
				,	scope: this
				, 	iconCls: 'icon_events_add'
				,	handler: this.addEvent
			}, {
					text: 'Edit Event'
				,	id: 'btn-edit-event'
				,	scope: this
				,	disabled: true
				, 	iconCls: 'icon_events_edit'
				,	handler: this.editEvent
			}, {
					text: 'Delete Event'
				,	id: 'btn-delete-event'
				,	scope: this
				,	disabled: true
				, 	iconCls: 'icon_events_delete'
				,	handler: function(){
						var record = this.grid.getSelectionModel().getSelected();
						if(!Ext.isEmpty(record) || Ext.isDefined(record)){
							this.grid.removeRequest(record.data);
						}else{
							Ext.Msg.alert('Notice', 'Please select event.');
						}
					}
			}, {
					id: 'view-grid'
				, 	iconCls: 'icon_events_grid'
				,	text: 'Grid View'
				,	handler: this.navHandler.createDelegate(this, ['grid'])
				,	disabled: true
			},
			{
					id: 'view-matrix'
				, 	iconCls: 'icon_events_matrix'
				,	text: 'Matrix View'
				,	handler: this.navHandler.createDelegate(this, ['matrix', this.matrix])
			}]
		,	items: [
					this.grid
				,	this.matrix
			]
	});

	Ext.apply(this, config, {
			layout: 'border'
		,	id: 'events-panel'
		,	hideMode: 'offsets'
		,	items: [
					card	
				,	this.frm
			]
	});

	SLM.EventsPanel.superclass.constructor.apply(this, arguments);

};

Ext.extend( SLM.EventsPanel, Ext.Panel, {
		
		navHandler: function( view, grid ) {
	
			switch( view ) {
				case 'matrix':
					Ext.getCmp('events-card').getLayout().setActiveItem(1);
					Ext.getCmp('view-grid').enable();
					Ext.getCmp('view-matrix').disable();					
					Ext.getCmp('btn-add-event').disable();
					Ext.getCmp('btn-edit-event').disable();
					Ext.getCmp('btn-delete-event').disable();
					grid.store.reload();
					break;
					
				case 'grid':
					Ext.getCmp('events-card').getLayout().setActiveItem(0);
					Ext.getCmp('view-matrix').enable();
					Ext.getCmp('btn-add-event').enable();
					this.grid.enableDisbleEvent();
					Ext.getCmp('view-grid').disable();
					break;
			}
			
		}

	,	addEvent: function() {
			var addEvent = new SLM.Event({'cmd':'add'});
			addEvent.on('saveSuccessful', function() {
				this.grid.store.reload();	
			}, this);
			addEvent.refStartDate.hideTrigger1 = true
			addEvent.refEndDate.hideTrigger1 = true
			addEvent.show();
		}

	,	editEvent: function() {
			var record = this.grid.getSelectionModel().getSelected();
			var addEvent = new SLM.Event({cmd:'edit', data: record.data});
			addEvent.on('saveSuccessful', function() {
				this.grid.store.reload();	
			}, this);
			addEvent.show();
		}

});